#include <QtTest>

// add necessary includes here
#include "CDatabaseManager.h"
#include "CTableHistory.h"
#include <QFile>
#include <QUuid>
class Table : public QObject
{
    Q_OBJECT

public:
    Table();
    ~Table();

private slots:
    void initTestCase();
    void cleanupTestCase();

    void testUpdateSignal(); //有崩溃概率

    void testAddAndQueryRow();
    void testDeleteRow();
    void testUpdateRow();

    void testQueryAll();
    void testQueryRows();
};

Table::Table()
{

}

Table::~Table()
{

}


void Table::initTestCase()
{
    //初始化数据库文件
    CDatabaseManager::instance()->init("./testTable.db");
}

void Table::cleanupTestCase()
{
//    CTableHistory::getInstance()->clearTable();
}

void Table::testUpdateSignal()
{
    bool haveAdd = false;
    bool haveUpdate = false;
    bool haveDelete = false;
    connect(CTableHistory::getInstance(),&CTableHistory::sigUpdate,this,
            [&](QVariant varId,CTableHistory::UpdateType type){
        qDebug()<<varId<<"Update type:"<<type;
        switch(type){
        case CTableHistory::Add:
            haveAdd = true;
            break;
        case CTableHistory::Update:
            haveUpdate = true;
            break;
        case CTableHistory::Delete:
            haveDelete = true;
            break;
        default:
            break;
        }
    });
    testAddAndQueryRow();
    testDeleteRow();
    testUpdateRow();
    QTRY_VERIFY(haveAdd && haveUpdate && haveDelete);
}

void Table::testAddAndQueryRow()
{
    QString varId = QUuid::createUuid().toString();

    RowHistory row;
    row.id = varId;
    row.date = 2022;
    row.info = "[INFO]";
    row.message = "hello world";
    row.dateTime = QDateTime::currentDateTime();
    QVERIFY(CTableHistory::getInstance()->addRow(row));

    RowHistory rowQuery;
    QVERIFY(CTableHistory::getInstance()->queryRow(varId,rowQuery));

    qDebug()<<"add dateTime:"<<row.dateTime<<"query dateTime:"<<rowQuery.dateTime;
    QCOMPARE(row.id,rowQuery.id);
    QCOMPARE(row.date,rowQuery.date);
    QCOMPARE(row.info,rowQuery.info);
    QCOMPARE(row.message,rowQuery.message);
    QCOMPARE(row.dateTime,rowQuery.dateTime);
}

void Table::testDeleteRow()
{
    QString varId = QUuid::createUuid().toString();

    RowHistory row;
    row.id = varId;
    row.date = 2022;
    row.info = "[INFO]";
    row.message = "test delete row";

    QVERIFY(CTableHistory::getInstance()->addRow(row));
    QVERIFY(CTableHistory::getInstance()->deleteRow(varId));
}

void Table::testUpdateRow()
{
    QString varId = QUuid::createUuid().toString();

    //添加一行
    RowHistory row;
    row.id = varId;
    row.date = 2022;
    row.info = "[INFO]";
    row.message = "need to be update";

    QVERIFY(CTableHistory::getInstance()->addRow(row));

    //更新
    row.date = 2021;
    row.info = "[DEBUG]";
    row.message = "update completed";
    QVERIFY(CTableHistory::getInstance()->updateRow(row));

    //查询 校验
    RowHistory rowQuery;
    QVERIFY(CTableHistory::getInstance()->queryRow(varId,rowQuery));
    QCOMPARE(row.id,rowQuery.id);
    QCOMPARE(row.date,rowQuery.date);
    QCOMPARE(row.info,rowQuery.info);
    QCOMPARE(row.message,rowQuery.message);
}

void Table::testQueryAll()
{
    //测试一
    QVector<RowHistory> vec;
    QVERIFY(CTableHistory::getInstance()->queryAll(vec));

    int beforeCnt = vec.count();

    //添加一行
    QString varId = QUuid::createUuid().toString();
    RowHistory row;
    row.id = varId;
    row.date = 2022;
    row.info = "[INFO]";
    row.message = QString("test query  all");
    QVERIFY(CTableHistory::getInstance()->addRow(row));
    QVERIFY(CTableHistory::getInstance()->queryAll(vec));

    QCOMPARE(beforeCnt + 1,vec.count());

    //测试二
    foreach(RowHistory row,vec){
        QVERIFY(!row.id.isEmpty());
    }

    //表行数查询
    QCOMPARE(CTableHistory::getInstance()->count(),vec.count());
}

void Table::testQueryRows()
{
    QVector<RowHistory> vec;
    QVERIFY(CTableHistory::getInstance()->queryRows(vec,5,2)); //从第二行开始查询5行数据
    qDebug()<<"query rows:"<<vec;
}

QTEST_APPLESS_MAIN(Table)

#include "tst_table.moc"
